package com.originskyseed.platform.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.CmsMailMapper;
import com.originskyseed.platform.cms.mapper.CmsMailRelFileMapper;
import com.originskyseed.platform.cms.mapper.CmsMailRelReceiveMapper;
import com.originskyseed.platform.cms.model.CmsMail;
import com.originskyseed.platform.cms.model.CmsMailRelFile;
import com.originskyseed.platform.cms.model.CmsMailRelReceive;
import com.originskyseed.platform.cms.service.ICmsMailService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 邮件信息表Service接口实现类（OSI_MAIL）
 *
 * @author wlj
 */
@DubboService
@Component("cmsMailService")
public class CmsMailServiceImpl implements ICmsMailService {

    @Resource
    private CmsMailMapper cmsMailMapper;

    @Resource
    private CmsMailRelReceiveMapper cmsMailRelReceiveMapper;

    @Resource
    private CmsMailRelFileMapper cmsMailRelFileMapper;

    /**
     * 删除记录
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByPrimaryKey(String id) throws Exception {
        return this.cmsMailMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除记录
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByPrimaryKeys(List<String> ids) throws Exception {
        return this.cmsMailMapper.deleteByPrimaryKeys(ids);
    }

    /**
     * 假删除记录
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByIds(List<String> ids) throws Exception {
        return this.cmsMailMapper.deleteByIds(ids);
    }

    /**
     * 插入记录
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    public int insert(CmsMail record) throws Exception {
        if (StringUtils.isBlank(record.getId())) {
            record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
        }
        record.setCreateTime(new Date());
        return this.cmsMailMapper.insert(record);
    }

    /**
     * 插入记录（仅非空值字段）
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    public int insertSelective(CmsMail record) throws Exception {
        if (StringUtils.isBlank(record.getId())) {
            record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
        }
        record.setCreateTime(new Date());
        return this.cmsMailMapper.insertSelective(record);
    }

    /**
     * 新增邮件
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    public int insertEmail(CmsMail record, UserInfo userInfo, List<CmsMailRelFile> cmsMailRelFileList, List<CmsMailRelReceive> cmsMailRelReceiveList) throws Exception {
        String key = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
        record.setId(key);
        record.setIsDel(2);
        record.setCreator(userInfo.getName());
        record.setCreatorId(userInfo.getCreatorId());
        if (cmsMailRelReceiveList != null && cmsMailRelReceiveList.size() > 0) {
            for (CmsMailRelReceive cmsMailRelReceive : cmsMailRelReceiveList) {
                cmsMailRelReceive.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
                cmsMailRelReceive.setMailId(key);
                cmsMailRelReceive.setIsRead(2);
                cmsMailRelReceive.setCreateTime(new Date());
            }
            this.cmsMailRelReceiveMapper.insertBatch(cmsMailRelReceiveList);
        }
        if (cmsMailRelFileList != null && cmsMailRelFileList.size() > 0) {
            for (CmsMailRelFile cmsMailRelFile : cmsMailRelFileList) {
                cmsMailRelFile.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				cmsMailRelFile.setMailId(record.getId());
                cmsMailRelFile.setCreateTime(new Date());
                this.cmsMailRelFileMapper.insertSelective(cmsMailRelFile);
            }
        }

        return this.insertSelective(record);
    }


    /**
     * 更新记录
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    public int updateByPrimaryKey(CmsMail record) throws Exception {
        record.setUpdateTime(new Date());
        return this.cmsMailMapper.updateByPrimaryKey(record);
    }

    /**
     * 更新记录（仅非空值字段）
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    public int updateByPrimaryKeySelective(CmsMail record) throws Exception {
        record.setUpdateTime(new Date());
        return this.cmsMailMapper.updateByPrimaryKeySelective(record);
    }

	/**
	 * 修改邮件
	 * @param record
	 * @param userInfo
	 * @param cmsMailRelFileList
	 * @param cmsMailRelReceiveList
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateEmail(CmsMail record, UserInfo userInfo, List<CmsMailRelFile> cmsMailRelFileList, List<CmsMailRelReceive> cmsMailRelReceiveList) throws Exception {

		//先删库数据
		this.cmsMailRelReceiveMapper.deleteByMailId(record.getId());
		if (cmsMailRelReceiveList != null && cmsMailRelReceiveList.size() > 0) {
			for (CmsMailRelReceive cmsMailRelReceive : cmsMailRelReceiveList) {
				cmsMailRelReceive.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				cmsMailRelReceive.setMailId(record.getId());
				cmsMailRelReceive.setIsRead(2);
				cmsMailRelReceive.setCreateTime(new Date());
			}
			this.cmsMailRelReceiveMapper.insertBatch(cmsMailRelReceiveList);
		}

		this.cmsMailRelFileMapper.deleteByMailId(record.getId());
		if (cmsMailRelFileList != null && cmsMailRelFileList.size() > 0) {
			for (CmsMailRelFile cmsMailRelFile : cmsMailRelFileList) {
				cmsMailRelFile.setMailId(record.getId());
				cmsMailRelFile.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				cmsMailRelFile.setCreateTime(new Date());
				this.cmsMailRelFileMapper.insertSelective(cmsMailRelFile);
			}
		}
		return this.updateByPrimaryKeySelective(record);
	}

	/**
     * 查询记录
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public CmsMail selectByPrimaryKey(String id) throws Exception {
        return this.cmsMailMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询列表
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    public List<CmsMail> selectList(CmsMail record) throws Exception {
        return this.cmsMailMapper.selectList(record);
    }

    /**
     * 分页查询列表
     *
     * @param params
     * @param page
     * @return
     * @throws Exception
     */
    @Override
    public BasePage<CmsMail> selectListByPage(Map<String, Object> params, BasePage<CmsMail> page) throws Exception {
        params.put("page", page);
        List<CmsMail> list = this.cmsMailMapper.selectListByPage(params);
        page.setResults(list);
        return page;
    }

}
